package com.jwgf.queueprogramflash;

import com.jwgf.smarowindingmachine_unity.logic.MachineParameterSharedPreference;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

public class JumpBootCallable implements Callable<List<Integer>> {
    private List<Integer> updateSpindles;

    public JumpBootCallable(List<Integer> updateSpindles) {
        this.updateSpindles = updateSpindles;
    }

    @Override
    public List<Integer> call() throws Exception {
        List<Integer> allSpindles = new ArrayList<>();
        for (int i = 2; i <= Integer.valueOf(MachineParameterSharedPreference.getInstance().getValue(0x29, 01)) + 1; ++i) {
            allSpindles.add(i);
        }
        for (Integer spindle : updateSpindles) {
            byte[] cmd = FlashCommand.createJumpBootFlash(spindle);
            FlashManager.getInstance().send(cmd);
        }
        List<Integer> noUpdateSpindles = new ArrayList<>(allSpindles);
        noUpdateSpindles.removeAll(updateSpindles);
        for (Integer spindle : noUpdateSpindles) {
            byte[] cmd = FlashCommand.createJumpBootNoFlash(spindle);
            FlashManager.getInstance().send(cmd);
        }

        Thread.sleep(3000);
        return FlashManager.getInstance().getEchoSpindles();
    }
}
